package com.douguo.recipe;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ProgressBar;

import com.douguo.recipe.view.NavigationView;

public class OAuthWebActivity extends DouguoActivity{
	private ProgressDialog progressDialog;
	
	private ProgressBar mProgressBar;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.oauth_web);
		String url = getIntent().getStringExtra("url");
		new NavigationView(this,NavigationView.STATE_NONE);
		findViewById(R.id.imageButton_back).setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				finish();
			}
		});
		mProgressBar = (ProgressBar) findViewById(R.id.ek_article_progress);
		
		WebView web = (WebView)findViewById(R.id.web_view);
		web.getSettings().setJavaScriptEnabled(true);
		web.getSettings().setSupportZoom(true);
		web.getSettings().setBuiltInZoomControls(true);
         
		web.loadUrl(url);
		web.setWebChromeClient(new WebChromeClient(){
			public void onProgressChanged(WebView view, int progress) {
	            //Activity和Webview根据加载程度决定进度条的进度大小
	           //当加载到100%的时候 进度条自动消失
				mProgressBar.setProgress(progress);
				if(progress == 100) {
					progressDialog.dismiss();
					mProgressBar.postDelayed(new Runnable() {
						
						@Override
						public void run() {
							mProgressBar.setVisibility(View.GONE);
							
						}
					}, 1000);
				}
			}
		});
		progressDialog = new ProgressDialog(
				OAuthWebActivity.this);
		progressDialog.setMessage("正在转至新浪微博授权页");
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDialog.setOnKeyListener(new OnKeyListener() {

			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					try {
						dialog.dismiss();
					} catch (Exception e) {
						// do nothing
					}
					return true;
				}
				return false;
			}
		});
		progressDialog.show();
	}

}
